package javaAdvanced;

import java.lang.ref.WeakReference;

/**
 * @author cuiH
 * Date: 13-10-24
 */
public class MyThreadLocal<T> {//传入的数据泛型
    /*
    * 模仿jdk进行map构造，以及clean code的原则构造
    * */

    //create a thread local variable,empty constructor
    public MyThreadLocal() {
    }

//    public void set(T value) {
//        Map map = new HashMap();
//        Thread t = Thread.currentThread();
//        ThreadLocalMap map = getMap(t);
//        if (map != null)
//            map.set(this, value);
//    }
//
//    public void set(ThreadLocal key, Object value) {
//        Map.Entry[] tab = table;
//        int len = tab.length;
//        int i = key.threadLocalHashCode & (len - 1);
//    }
//
//    ThreadLocalMap getMap(Thread t) {
//        return t.threadLocals;
//    }

    /**
     * ThreadLocalMap 是一个传统的哈希map，仅仅为了维持thread local 的值，主要是
     * 防止数值过大
     */
    static class ThreadLocalMap {
        static class Entry extends WeakReference<ThreadLocal> {
            Object value;

            Entry(ThreadLocal k, Object v) {
                super(k);
                value = v;
            }
        }
    }
}
